package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.BatchDeleteProductsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BatchDeleteProducts {
	private static BatchDeleteProductsCall batchDeleteProductsCall = new BatchDeleteProductsCall();
	
	
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量删除商品");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchDeleteProductsCall.getOpurl());
	}
	
	

	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_001() {
		Logger.start(true,"删除多个商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();	
			
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,Arrays.asList(productId1,productId2));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_002() {
		Logger.start(true,"删除多个商品,包含正在进行直播商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getLiveProduct(sellerId, sellerName).getProductId();
			
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,MapUtil.hashMap(productId1, false, productId2, true));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_003() {
		Logger.start(true,"删除多个商品,包含将来直播商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getFutureLiveProduct(sellerId,sellerName).getProductId();
			
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,MapUtil.hashMap(productId1, false, productId2, true));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_004() {
		Logger.start(true,"删除多个商品,包含活动商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getActivityProduct(sellerId, sellerName).getProductId();
			
			ymtProductActivityStockWapper.updateStartTimeByProductId(productId2,  "2016-09-28 17:27:39.000");
			
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,Arrays.asList(productId1));
			
			
			
			YmtProductsI productInSql = ymtProductsIWapper.selectProductByProductId(productId2).get(0);
			
			Logger.verifyEquals(result.getJSONObject("Data").getJSONArray("CantDeleteProductsNames").getString(0), productInSql.getsProduct(), "活动商品不能被删除");
			Logger.verifyEquals(0, productInSql.getiAction(), "活动商品不能被删除");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_005() {
		Logger.start(true,"删除多个商品,包含将来开始活动商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getFutureActivityProduct(sellerId, sellerName, null).getProductId();
			
			
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,Arrays.asList(productId1));
			
			
			
			YmtProductsI productInSql = ymtProductsIWapper.selectProductByProductId(productId2).get(0);
			
			Logger.verifyEquals(result.getJSONObject("Data").getJSONArray("CantDeleteProductsNames").getString(0), productInSql.getsProduct(), "活动商品不能被删除");
			Logger.verifyEquals(0, productInSql.getiAction(), "活动商品不能被删除");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_006() {
		Logger.start(true,"删除多个商品,包含已结束活动商品");
		try {
			String productId1 =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			String productId2 =  ProductFactory.getPreviousActivityProduct(sellerId, sellerName, null).getProductId();
			
			YMTDateUtil.waitTime(2);
			JSONObject result = batchDeleteProductsCall.call(sellerId,Arrays.asList(productId1,productId2));
			
			checkDeleteProduct(result,Arrays.asList(productId1,productId2));
		
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_007() {
		Logger.start(true,"删除不属于该买手商品");
		try {
			String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName).getProductId();
			
			batchDeleteProductsCall.call(sellerId1,Arrays.asList(productId));
			
			YmtProductsI productInSql = ymtProductsIWapper.selectProductByProductId(productId).get(0);
			Logger.verifyEquals(0, productInSql.getiAction(), "不属于该买手商品不能删除");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public static void checkDeleteProduct(JSONObject ret,List<String> productIdList) throws Exception{
		Map<String,Boolean> productIdAndIsInLive = MapUtil.hashMap();
		productIdList.forEach(x->productIdAndIsInLive.put(x, false));
		checkDeleteProduct(ret,productIdAndIsInLive);
	}
	
	
	
	@SuppressWarnings("rawtypes")
	public static void checkDeleteProduct(JSONObject ret, Map<String,Boolean> productIdAndIsInLive) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回code应为200");
		JSONArray actArray = ret.getJSONObject("Data").getJSONArray("CantDeleteProductsNames");
		List<String> cantDeleteProduct = new ArrayList<>();
		for(int i=0;i<actArray.length();i++){
			cantDeleteProduct.add(actArray.getString(i));
		}
		
		
		
		productIdAndIsInLive.forEach((productId,isInLive)->{
			try {
				Logger.verifyEquals(false, cantDeleteProduct.contains(productId),"返回报文中CantDeleteProductsNames 不该包含 productid:"+productId);
				List<Map> productIdsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
				Logger.verifyEquals(-2, Integer.parseInt(productIdsqllist.get(0).get("iAction").toString()), "删除商品 iaction为-1");
	
				
				if (isInLive){
					List<Map> liveProductMapList = ymtProductsInLiveWapper.selectInLiveByProductId(productId);
					for(int j=0;j<liveProductMapList.size();j++){
						Map liveProductMap = liveProductMapList.get(j);
						int liveId = Integer.parseInt(liveProductMap.get("iActivityId").toString());
						
						Logger.verifyEquals(-1, Integer.parseInt(liveProductMap.get("iAction").toString()), "删除商品，iaction应为-1 liveId:"+ liveId
								+", productId:" + productId);
						
						//默认删除后，直播中没有有效商品
						Map liveMap = ymtLiveWapper.selectforMapByLiveId(liveId).get(0);
						Logger.verifyEquals(0, Integer.parseInt(liveMap.get("ProductCount").toString()), "删除后, productcount应为0,liveId:"+ liveId);
						Logger.verifyEquals(0, Integer.parseInt(liveMap.get("PutawayCount").toString()), "删除后, PutawayCount应为0,liveId:"+ liveId);
						
						NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId,false);
					}
				}
				
				
				
				//check mongo
				NewJavaMongoCheckService.checkProductAsDeleteMgoSyncBySQL_NewJavaMgo(productId);
			
			
			}catch(Exception e){
				Logger.fail(e);
			}
			
		});
		
		
		
		
		
		
		
		
		
	}
	
	
	
	
	
	
}
